<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java并发编程：synchronized vs ReentrantLock深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4F46E5;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 代码块美化 */
        pre {
            background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            position: relative;
        }
        
        pre::before {
            content: '';
            position: absolute;
            top: 10px;
            left: 15px;
            width: 12px;
            height: 12px;
            background: #ff5f56;
            border-radius: 50%;
            box-shadow: 20px 0 0 #ffbd2e, 40px 0 0 #27c93f;
        }
        
        code {
            color: #e2e8f0;
            font-family: 'Fira Code', monospace;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        
        /* 表格美化 */
        table {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
        }
        
        th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
            padding: 1rem;
        }
        
        td {
            padding: 1rem;
            border-bottom: 1px solid #e5e7eb;
        }
        
        tr:hover {
            background: #f9fafb;
            transition: background 0.3s ease;
        }
        
        /* 卡片悬停效果 */
        .card {
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }
        
        /* 标题装饰 */
        .section-title {
            position: relative;
            padding-left: 1.5rem;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 5px;
            height: 80%;
            background: linear-gradient(180deg, #4F46E5 0%, #7C3AED 100%);
            border-radius: 3px;
        }
        
        /* 引用样式 */
        .quote-box {
            position: relative;
            padding: 2rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 12px;
            margin: 2rem 0;
        }
        
        .quote-box::before {
            content: '"';
            position: absolute;
            top: -20px;
            left: 20px;
            font-size: 5rem;
            opacity: 0.3;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 动画效果 */
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
        
        /* 高亮标记 */
        .highlight {
            background: linear-gradient(180deg, transparent 60%, #fbbf24 60%);
            padding: 0 0.2rem;
        }
        
        /* Mermaid图表样式 */
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="relative overflow-hidden bg-gradient-to-br from-purple-600 via-indigo-600 to-blue-600 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative max-w-7xl mx-auto px-4 py-24 sm:px-6 lg:px-8">
            <div class="text-center animate-fade-in">
                <h1 class="text-5xl md:text-7xl font-bold mb-6 tracking-tight">
                    Java并发编程的艺术
                </h1>
                <p class="text-2xl md:text-3xl mb-8 font-light">
                    synchronized vs ReentrantLock 深度解析
                </p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-lock mr-2"></i>
                        <span>锁机制对比</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-chart-line mr-2"></i>
                        <span>性能分析</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        <span>实战案例</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 105C120 90 240 60 360 45C480 30 600 30 720 37.5C840 45 960 60 1080 67.5C1200 75 1320 75 1380 75L1440 75V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0Z" fill="white"/>
            </svg>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-4 py-12">
        <!-- 开篇故事 -->
        <section class="mb-16 animate-fade-in">
            <h2 class="text-4xl font-bold mb-8 text-gray-800 section-title">
                <i class="fas fa-store mr-3 text-indigo-600"></i>开篇故事：超市储物柜的启示
            </h2>
            <div class="bg-white rounded-2xl shadow-xl p-8 card">
                <p class="text-lg text-gray-700 mb-6 drop-cap">
                    假设你第一次去超市购物，发现两种储物柜：普通储物柜就像synchronized，投入硬币自动上锁，取出物品自动开锁，简单直接；而智能储物柜则像ReentrantLock，支持指纹解锁、预约柜门、查看使用记录，功能强大但需要更多操作。
                </p>
                <div class="grid md:grid-cols-2 gap-6 mt-8">
                    <div class="bg-gradient-to-br from-blue-50 to-indigo-100 p-6 rounded-xl">
                        <h3 class="text-xl font-bold text-indigo-800 mb-3">
                            <i class="fas fa-lock mr-2"></i>普通储物柜（synchronized）
                        </h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i>投币即锁，自动管理</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i>操作简单，无需学习</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i>适合日常使用</li>
                        </ul>
                    </div>
                    <div class="bg-gradient-to-br from-purple-50 to-pink-100 p-6 rounded-xl">
                        <h3 class="text-xl font-bold text-purple-800 mb-3">
                            <i class="fas fa-fingerprint mr-2"></i>智能储物柜（ReentrantLock）
                        </h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i>指纹识别，安全可控</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i>预约功能，灵活管理</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i>使用记录，状态可查</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- 血泪史 -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-gray-800 section-title">
                <i class="fas fa-exclamation-triangle mr-3 text-red-600"></i>血泪史：618大促的库存超卖事故
            </h2>
            <div class="quote-box">
                <p class="text-xl font-light">
                    某电商平台在618大促期间，100台特价手机竟然卖出了123台！事后排查发现是并发锁机制使用不当导致。
                </p>
            </div>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mt-8">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-bug mr-2 text-red-500"></i>问题代码重现
                </h3>
                <pre><code class="language-java">public class DisasterService {
    private int stock = 100;
    
    // 问题代码：天真的锁使用方式
    public void purchase() {
        if(stock > 0) {
            try {
                Thread.sleep(10); // 模拟网络延迟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            stock--;
        }
    }
}</code></pre>
                
                <div class="mt-8 bg-red-50 border-l-4 border-red-500 p-6 rounded-r-lg">
                    <h4 class="text-xl font-bold text-red-800 mb-3">
                        <i class="fas fa-microscope mr-2"></i>问题分析
                    </h4>
                    <p class="text-gray-700 mb-4">当1000个用户同时抢购时，会发生以下情况：</p>
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <span class="bg-red-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">1</span>
                            <p>线程A检查库存为100</p>
                        </div>
                        <div class="flex items-start">
                            <span class="bg-red-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">2</span>
                            <p>线程B也检查库存为100</p>
                        </div>
                        <div class="flex items-start">
                            <span class="bg-red-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">3</span>
                            <p>两个线程都进入购买流程</p>
                        </div>
                        <div class="flex items-start">
                            <span class="bg-red-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">4</span>
                            <p>最终库存变成98（实际应该减少2）</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- synchronized解决方案 -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 text-gray-800 section-title">
                <i class="fas fa-shield-alt mr-3 text-blue-600"></i>synchronized解决方案：简单可靠的"自动锁"
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-2xl shadow-xl p-8">
                    <h3 class="text-